const api = require('../../utils/api')
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: '',
    option1: [
      { text: '全部', value: '' },
      { text: '堂食', value: 1 },
      { text: '外带', value: 2 },
    ],
    option2: [
      { text: '全部', value: '' },
      { text: '已下单', value: 1 },
      { text: '已取消', value: 2 },
      { text: '已评价', value: 3 }
    ],
    option3: [
      { text: '全部', value: '' },
      { text: '午餐', value: 1 },
      { text: '晚餐', value: 2 }
    ],
    orderType: '',
    orderStatus: '',
    cookbookType: '',
    orderList: [],
    pageNum: 1,
    pageSize: 15,
    total: ''
  },

  loadOrderList () {
    api.get(`/order?cookbookType=${this.data.cookbookType}&orderStatus=${this.data.orderStatus}&orderType=${this.data.orderType}&pageNum=${this.data.pageNum}&pageSize=${this.data.pageSize}`).then(res => {
      if (res.code === 200) {
        this.setData({
          orderList: this.data.orderList.concat(res.data.records),
          total: res.data.total
        })
      }
    })
  },

  changeOrderType (e) {
    this.setData({
      activeName: '',
      orderList: [],
      pageNum : 1,
      orderType: e.detail
    })
    this.loadOrderList()
  },

  changeOrderStatus (e) {
    this.setData({
      activeName: '',
      orderList: [],
      pageNum : 1,
      orderStatus: e.detail
    })
    this.loadOrderList()
  },

  changeCookbookType (e) {
    this.setData({
      activeName: '',
      orderList: [],
      pageNum : 1,
      cookbookType: e.detail
    })
    this.loadOrderList()
  },

  changeCollapse (e) {
    this.setData({
      activeName: e.detail
    })
  },

  cancelOrder (e) {
    const orderId = e.currentTarget.dataset.order.id
    Dialog.confirm({
      title: '提示',
      message: '确定取消订单？',
    }).then(() => {
      api.put(`/order/cancel/${orderId}`).then(res => {
        if (res.code === 200) {
          this.setData({
            orderList: [],
            pageNum: 1
          })
          this.loadOrderList()
        }
      })
    }) .catch(() => {
      // on cancel
    });
    
  },

  deleteOrder (e) {
    const orderId = e.currentTarget.dataset.order.id
    Dialog.confirm({
      title: '提示',
      message: '确定删除订单？',
    }).then(() => {
      api.del(`/order/${orderId}`).then(res => {
        if (res.code === 200) {
          this.setData({
            orderList: [],
            pageNum: 1
          })
          this.loadOrderList()
        }
      })
    }) .catch(() => {
      // on cancel
    });
  },

  commentOrder (e) {
    const order = e.currentTarget.dataset.order
    wx.navigateTo({
      url: '/pages/comment/comment',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: order })
      }
    })
  },

  viewOrder (e) {
    const order = e.currentTarget.dataset.order
    wx.navigateTo({
      url: '/pages/comment/comment',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: order })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadOrderList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      orderList: [],
      pageNum : 1
    })
    this.loadOrderList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if ((this.data.pageNum * this.data.pageSize) > this.data.total) {
      return;
    }
    this.setData({
      pageNum : this.data.pageNum +  1
    })
    this.loadOrderList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})